package com.dhnsoft.数据结构与算法.栈与队列.栈;


import java.util.Scanner;

/**
 * ClassName: ArrayStackDemo
 * Package: com.dhnsoft.数据结构与算法.栈与队列.栈
 * Description:
 *
 * @Author dhn
 * @Create 2024/3/22 10:56
 * @Version 1.0
 */
public class ArrayStackDemo2 {
    public static void main(String[] args) {
        String expression = "80+2*6-4";
        ArrayStack2 numStack = new ArrayStack2(10);
        ArrayStack2 operStack = new ArrayStack2(10);
        int index = 0;
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        int res = 0;
        char ch=' ';
        String keepNum="";
        while (true){
            ch = expression.substring(index,index+1).charAt(0);
            if(operStack.isOper(ch)){
                if(!operStack.isEmpty()){
                    if(operStack.priority(ch)<=operStack.priority(operStack.peek())){
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        oper = operStack.pop();
                        res = numStack.cal(num1,num2,oper);
                        numStack.push(res);
                        operStack.push(ch);
                    }
                    else{
                        operStack.push(ch);
                    }
                }
                else{
                    operStack.push(ch);
                }
            }
            else{
                keepNum += ch;
                if(index == expression.length()-1){
                    numStack.push(Integer.parseInt(keepNum));
                }
                else{
                    if(operStack.isOper(expression.substring(index+1,index+2).charAt(0))){
                        numStack.push(Integer.parseInt(keepNum));
                        keepNum = "";
                    }
                }
//                numStack.push(ch-48);
            }
            index ++;
            if(index>=expression.length()){
                break;
            }
        }


        while (true){
            if(operStack.isEmpty()){
                break;
            }
            num1 = numStack.pop();
            num2 = numStack.pop();
            oper = operStack.pop();
            res = numStack.cal(num1,num2,oper);
            numStack.push(res);
        }
        System.out.printf("表达式%s=%d",expression,numStack.pop());
    }
}

//定义一个ArrayStack表示栈
class ArrayStack2{
    private int maxSize;

    private int[] stack;

    private int top = -1;

    public ArrayStack2(int maxSize){
        this.maxSize = maxSize;
        stack = new int[maxSize];
    }

    //判断栈是否为空
    public boolean isEmpty(){
        return top==-1;
    }

    //判断栈是否满
    public boolean isFull(){
        return top == maxSize-1;
    }


    public int pop(){
        if(isEmpty()){
            throw new RuntimeException("栈为空");
        }
        int temp = stack[top];
        top--;
        return temp;
    }

    public void push(int n){
        if(isFull()){
            System.out.println("栈满了");
            return;
        }
        stack[++top] = n;
    }

    public void showStack(){
        if(isEmpty()){
            throw new RuntimeException("栈为空");
        }
        for (int i=maxSize-1;i>=0;i--){
            System.out.printf("stack[%d]=%d\n",i,stack[i]);
        }
    }

    //返回运算符的优先级
    public int priority(int oper){
        if(oper=='+'||oper=='-'){
            return 0;
        }
        else if (oper=='*'||oper=='/'){
            return 1;
        }
        else{
            return -1;
        }
    }

    public int peek(){
        return stack[top];
    }

    //判断是否为运算符
    public boolean isOper(char oper){
        return oper=='*'||oper=='/'||oper=='+'||oper=='-';
    }

    //计算方法
    public int cal(int num1,int num2,int oper){
        int res = 0;
        switch (oper){
            case '+':
                res = num1+num2;
                break;
            case '-':
                res = num2-num1;
                break;
            case '*':
                res = num1*num2;
                break;
            case '/':
                res = num2/num1;
                break;
            default:
                break;
        }
        return res;
    }

}